﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;

namespace v1_SweetGeeks.View
{
    public class TileInfo
    {
        public double x1, y1, x2, y2;
        private double toX1, toY1, toX2, toY2, startX1, startY1, startX2, startY2;
        public Image img;
        private bool sourceControl;
        private string path;
        public bool is_image;
        public bool is_text;
        public Brush brush;
        public double opacity;
        public string text;
        private bool locked;
        public bool moving;

        public TileInfo(double x1, double y1, double x2, double y2) 
        {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
            toX1 = x1;
            toY1 = y1;
            toX2 = x2;
            toY2 = y2;
            sourceControl = false;
            is_image = false;
            is_text = false;
            brush = Draw.defualtBrush;
            opacity = 1.0;
            moving = false;
        }

        public TileInfo(double x1, double y1, string text)
        {
            this.x1 = x1;
            this.y1 = y1;
            x2 = 0.0;
            y2 = 0.0;
            toX1 = x1;
            toY1 = y1;
            toX2 = x1;
            toY2 = y1;
            sourceControl = false;
            is_image = false;
            is_text = true;
            this.text = text;
            brush = Brushes.White;
            opacity = 1.0;
            moving = false;
        }

        public void SetImage(string path)
        {
            do
            {
                if(!locked)
                {
                    locked = true;
                    this.path = path;
                    sourceControl = true;
                    is_image = true;
                    locked = false;
                }
            }while(locked);
        }

        public void Setdestination(double x_1, double y_1, double x_2, double y_2)
        {
            do
            {
                if(!locked)
                {
                    locked = true;
                    startX1 = x1;
                    startY1 = y1;
                    startX2 = x2;
                    startY2 = y2;

                    toX1 = x_1;
                    toY1 = y_1;
                    toX2 = x_2;
                    toY2 = y_2;
                    locked = false;
                }
            }while(locked);
        }

        public void Update()
        {
            do
            {
                if(!locked)
                {
                    locked = true;
                    if(sourceControl)
                    {
                        img = new Image();
                        try
                        {
                            img.Source = (ImageSource)new ImageSourceConverter().ConvertFromString(path);
                        }
                        catch
                        {
                            img.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("error.png");
                        }
                        sourceControl = false;
                    }
                    int old_x1 = (int)x1;
                    int old_x2 = (int)x2;
                    int old_y1 = (int)y1;
                    int old_y2 = (int)y2;
                    x1 = Draw.CurveValue(startX1, x1, toX1, 1.5);
                    y1 = Draw.CurveValue(startY1, y1, toY1, 1.5);
                    x2 = Draw.CurveValue(startX2, x2, toX2, 1.5);
                    y2 = Draw.CurveValue(startY2, y2, toY2, 1.5);
                    moving = old_x1 != (int)x1 || old_x2 != (int)x2 || old_y1 != (int)y1 || old_y2 != (int)y2 ? true : false;
                    locked = false;
                }
            }while(locked);
        }
    }

    public static class Tile
    {
        public static List<TileInfo> tiles = new List<TileInfo>(0);
        
        public static bool Moving()
        {
            bool movement = false;

            for(int i = 0; i < tiles.Count && !movement; i++)
            {
                if(tiles[i].moving)
                {
                    movement = true;
                }
            }

            return movement;
        }
    }
}
